package edu.lfsfxy.controller;

import edu.lfsfxy.dao.MeetDao;
import edu.lfsfxy.entity.Meet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/meet")
public class MeetController {
    @Autowired
    MeetDao meetDao;
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Meet> list = meetDao.select();
        model.addAttribute("list",list);
        return "list";
    }
    @RequestMapping("/add")
    public String add(Meet meet,Model model){
        try {
            int i = meetDao.add(meet);
            if(i>0){
                model.addAttribute("msg","添加成功");
            }else {
                model.addAttribute("msg","添加失败");
            }
        } catch (Exception e) {
            model.addAttribute("msg","添加错误："+e.getMessage());
        }
        return "redirect:/meet/findAll";
    }

    @RequestMapping("/delete")
    public String delete(Meet meet,Model model){
        try {
            int i = meetDao.delete(meet);
            if(i>0){
                model.addAttribute("msg","删除成功");
            }else{
                model.addAttribute("msg","删除失败");
            }
        } catch (Exception e) {
            model.addAttribute("msg","删除错误："+e.getMessage());
        }
        return "redirect:/meet/findAll";
    }
    @RequestMapping("/selectName")
    public String selectName(Model model){
        List<Meet> name = meetDao.selectName();
        model.addAttribute("name",name);
        return "add";
    }
    @RequestMapping("/selectN")
    public String selectN(Meet m,Model model){
        Meet meet = meetDao.selectN(m);
        List<Meet> name = meetDao.selectName();
        model.addAttribute("name",name);
        model.addAttribute("meet",meet);
        return "update";
    }
    @RequestMapping("/update")
    public String update(Meet m,Model model){
        int i = meetDao.update(m);
        if(i>0){
            model.addAttribute("msg","编辑成功");
        }else{
            model.addAttribute("msg","编辑失败");
        }
        return "redirect:/meet/findAll";
    }
}
